package edu.stanford.bmir.protege.web.server.stkosutil.enricher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.semanticweb.owlapi.model.EntityType;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLObjectProperty;

import edu.stanford.bmir.protege.web.client.rpc.stkosutil.enricher.CandidateEntity;
import edu.stanford.bmir.protege.web.server.change.ChangeGenerationContext;
import edu.stanford.bmir.protege.web.server.change.ChangeListGenerator;
import edu.stanford.bmir.protege.web.server.change.OntologyChangeList;
import edu.stanford.bmir.protege.web.server.owlapi.OWLAPIProject;
import edu.stanford.bmir.protege.web.server.owlapi.RenameMap;
import edu.stanford.bmir.protege.web.shared.DataFactory;

public class CandidateOPChangeGenerator implements ChangeListGenerator<Set<OWLObjectProperty>>{
	private OWLObjectProperty _parentProperty = null;
	private ArrayList<CandidateEntity> _alCandidateEntitys = new ArrayList<CandidateEntity>();
	private HashMap<OWLObjectProperty, String> _hFreshPropertyList = null;
	public CandidateOPChangeGenerator(OWLObjectProperty parentProperty, ArrayList<CandidateEntity> alCandidateEntitys){
		this._parentProperty = parentProperty;
		this._alCandidateEntitys = alCandidateEntitys;
	}
	public OntologyChangeList<Set<OWLObjectProperty>> generateChanges(OWLAPIProject project, ChangeGenerationContext context) {
		HashSet<OWLObjectProperty> sFreshPropertyList = new HashSet<OWLObjectProperty>();
		_hFreshPropertyList = new HashMap<OWLObjectProperty, String>();
		OntologyChangeList.Builder<Set<OWLObjectProperty>> builder = new OntologyChangeList.Builder<Set<OWLObjectProperty>>();
		for (CandidateEntity candidateEntity: _alCandidateEntitys){
			OWLObjectProperty freshProperty = DataFactory.getFreshOWLEntity(EntityType.OBJECT_PROPERTY, candidateEntity.getBrowserText());	
				OWLDataFactory df = DataFactory.get();
				builder.addAxiom(project.getRootOntology(), df.getOWLDeclarationAxiom(freshProperty));
				builder.addAxiom(project.getRootOntology(), df.getOWLSubObjectPropertyOfAxiom(freshProperty, _parentProperty));
	 			sFreshPropertyList.add(freshProperty);
	 			this._hFreshPropertyList.put(freshProperty, candidateEntity.getSourceLabel());
		}
		return builder.build(sFreshPropertyList);
	}
	
	public Set<OWLObjectProperty> getRenamedResult(Set<OWLObjectProperty> result, RenameMap renameMap) {
	        return renameMap.getRenamedEntities(result);
	}    
	public HashMap<OWLObjectProperty, String> getFresshPropertyList(){
		return this._hFreshPropertyList;
	}
}
